<template>
  <div>
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item label="部门名称" label-width="100px" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" label-width="100px" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" label-width="100px" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择">
          <el-option
            v-for="item in simpleList"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" label-width="100px" prop="introduce">
        <el-input v-model="form.introduce" />
      </el-form-item>
      <el-form-item style="text-align:center">
        <el-button @click="doCancel">取消</el-button>
        <el-button type="primary" @click="doAdd">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { SimpleEmployee, addSubject, departmentInfo, editSubject } from '@/api/departments'
export default {
  props: {
    // 点击新增部门的id
    pid: {
      type: String,
      required: true
    },
    // 用来区别点击的是新增还是编辑
    edit: {
      type: Boolean,
      required: true
    },
    // 部门数据
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 1.code数据校验
    const validCode = (rule, value, callback) => {
      // 获取所有部门的code
      let code = this.originList.map(item => item.code)
      if (this.edit) {
        // 如果是编辑框，筛选出除自己之外的所有code值
        code = this.originList.filter(item => item.id !== this.pid).map(item => item.code)
        console.log(code)
      }
      // 判断code数组里面，是否有输入的value值
      code.includes(value) ? callback(new Error(value + '已经被占用')) : callback()
    }
    // 2.name数据校验
    const validName = (rule, value, callback) => {
      // 获取被点击部门的子部门的name
      let nameList = this.originList.filter(item => item.pid === this.pid).map(item => item.name)
      if (this.edit) {
        // 获取被点击部门的pid
        const item = this.originList.find(item => item.id === this.pid).pid
        // 筛选兄弟部门，即pid相等的部门，而且要排除自己，剩下的就是被占用的name
        nameList = this.originList.filter(ele => item === ele.pid && this.pid !== ele.id).map(item => item.name)
      }
      nameList.includes(value) ? callback(new Error(value + '已经被占用')) : callback()
    }
    return {
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      // 新增部门的数据
      form: {
        name: '',
        introduce: '',
        manager: '',
        code: ''
      },
      // 部门负责人信息
      simpleList: []
    }
  },
  // watch: {
  //   pid: {
  //     immediate: true,
  //     handler(_newVal, _oldVal) {
  //       if (this.edit) {
  //         this.getDepartment()
  //       }
  //     }
  //   }
  // },
  created() {
    // 如果edit是true，则父组件点击的是编辑按钮，需要获取部门信息
    // if (this.edit) {
    //   this.getDepartment()
    // }
    this.doSimpleEmployee()
  },
  methods: {
    // 1.发送ajax获取部门负责人
    async doSimpleEmployee() {
      const res = await SimpleEmployee()
      this.simpleList = res.data
    },
    // 2.点击编辑部门，需要请求ajax获取部门信息，渲染到页面
    async getDepartment() {
      const res = await departmentInfo(this.pid)
      this.form = res.data
    },
    // 3.新增部门，发送ajax
    doAdd() {
      this.$refs.formRef.validate(async(valid) => {
        if (valid) {
          if (this.edit) {
            await editSubject(this.form)
            // 子传父，修改弹窗show值，重新调用渲染页面函数
            this.$emit('success')
          } else {
            await addSubject({ ...this.form, pid: this.pid })
            // 子传父，修改弹窗show值，重新调用渲染页面函数
            this.$emit('success')
          }
        } else {
          return false
        }
      })
    },
    // 4.点击取消
    doCancel() {
      this.$emit('close')
    },
    // 5.重置表单
    reset() {
      this.$refs.formRef.resetFields()
    }
  }

}
</script>

<style>

</style>
